package com.millstein.controller;

import com.millstein.request.UserLoginRequest;
import com.millstein.request.UserRegisterRequest;
import com.millstein.service.FileService;
import com.millstein.service.UserService;
import com.millstein.util.JsonData;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author millstein
 * @since 2021-09-08
 */
@RestController
@RequestMapping("/api/user/v1")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FileService fileService;

    /**
     * 用户头像上传
     *      这里注意，如果上传文件的接口要和swagger连用，那么只能使用@RequestPart而不能不是@RequestParam
     * @param file 上传的文件
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("用户头像上传")
    public JsonData uploadAvatar(@ApiParam(value = "上传的文件信息", required = true) @RequestPart("file") MultipartFile file) {
        return JsonData.buildSuccess(fileService.uploadImage(file));
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public JsonData register(@ApiParam("用户注册对象") @RequestBody UserRegisterRequest userRegisterRequest) {
        userService.register(userRegisterRequest);
        return JsonData.buildSuccess();
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public JsonData login(@ApiParam("用户登录对象") @RequestBody UserLoginRequest userLoginRequest) {
        String token = userService.login(userLoginRequest);
        return JsonData.buildSuccess(token);
    }

    @GetMapping("/detail")
    @ApiOperation("查询用户自己的信息")
    public JsonData findDetail() {
        return JsonData.buildSuccess(userService.findDetail());
    }

}

